module.exports = function ($scope, $resource, $modalInstance, toaster, obj, flag) {

    init();
    var temp = ''
    function init(){
        $scope.buy = flag;
        $scope.package = obj;
        console.log($scope.package);
        $scope.package.price_level0 = 0;
        $scope.fromDate = {
            'pay_type': '1',
        }
        // temp = $scope.package.price_level3 || $scope.package.price_level2 || $scope.package.price_level1;
        // console.log(temp);
        // $dom = $('.a'+temp);
        // console.log($dom);
        // $dom.addClass('selected');
        
    }
    //选择时间
    $scope.selectDate = function(period, target){
      $('.pay-style').removeClass('selected');
      var $dom = $(target);
      $dom.addClass('selected');
      // $dom = $('.a500');
      //     console.log($dom);
      //     $dom.addClass('selected');
      $scope.fromDate.period = period;
    }
    // 选择支付方式
    // $scope.selectPay = function(period, target){
    //     $('.btn-style').removeClass('selected');
    //     $dom = $(target);
    //     $dom.addClass('selected');
    //     $scope.fromDate.period = period;
    // }
    $scope.ok = function(){
        if(!$scope.fromDate.period){
            toaster.warning({title: '', body: '请选择购买期限'});
            return;
        }
        if(!$scope.fromDate.pay_type){
            toaster.warning({title: '', body: '请选择支付方式'});
            return;
        }

        var para = {
            'code': obj.code,
            'pay_type': $scope.fromDate.pay_type,
            'period': $scope.fromDate.period,
            'invitation_code' : $scope.fromDate.invitation_code
        }

        $resource('/api/ac/pc/packageService/setBuyPackage', {}, {}).save(para, function(res){
            if (res.errcode === 0){
                toaster.success({title: '', body: '服务开通成功'});
                $modalInstance.close();
            } else {
                toaster.error({title: '', body: res.errmsg});
            }
        });
    }

    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    }


};